﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Inscricoes.Web.Cursos
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    Populate();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void Populate()
        {
            gvList.DataSource = Inscricoes.Business.Curso.SelectOrderingByName();
            gvList.DataBind();
        }

        protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                var obj = new Inscricoes.Business.Curso(Convert.ToInt32(gvList.DataKeys[e.RowIndex].Value));
                obj.Delete();

                Populate();
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void SetDeleteConfirmation(TableRow row)
        {
            foreach (Control c in row.Cells[0].Controls)
            {
                if (c is LinkButton)
                {
                    LinkButton btn = (LinkButton)c;
                    if (btn.CommandName == DataControlCommands.DeleteCommandName)
                    {
                        btn.OnClientClick = "return confirm('Deseja remover este item?');";
                    }
                }
            }
        }

        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            SetDeleteConfirmation(e.Row);
        }
    }
}
